package org.chen.spring.core.io;


import java.io.FileNotFoundException;
import java.net.URL;
import java.util.*;

/**
 * 资源文件加载工厂
 *
 * @author Chen
 * @since 2023/6/7 14:46
 */
public class ResourceLoaderFactory {

    private static final String RESOURCE_YML = "yml";

    private static final String RESOURCE_YAML = "yaml";

    private static final String RESOURCE_PROPERTIES = "properties";


    private final Map<String, ResourceLoader> resourceLoader;

     {
        resourceLoader = new HashMap<>(4);
        resourceLoader.put(RESOURCE_YML, new YmlResourceLoader());
    }


    public static void loaderResource(List<String> packageNameList) {
        ClassLoader classLoader = ResourceLoaderFactory.class.getClassLoader();
        ResourceLoaderFactory resourceLoaderFactory = new ResourceLoaderFactory();
        resourceLoaderFactory.resourceLoader.forEach((k, v) -> {
            URL resource = classLoader.getResource("application." + k);
            if (resource != null) {
                try {
                    v.getResource(resource, packageNameList);
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

}
